<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$sale_live=$this->plugin_setting('sale_live');
	$live_id=intval($_RQ['live_id']);
	if($live_id){
		$this->session('live_id',$live_id);
	}
	if($_SESSION['wxapp_id']){
		$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$this->session('live_id',$live_id);
	}
	$live_id=$this->session('live_id');
	if(!$live_id){
		if($sale_live['live_id']){
			$live_id=$sale_live['live_id'];
		}else{
			$live_id=pdo_getcolumn('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$this->session('live_id',$live_id);
	}
	$this->model('live')->checkendtime($live_id);
	if($_RQ['fromroomid'] || $_RQ['room_id']){
		$return['fromroomid']=$_RQ['fromroomid']?:$_RQ['room_id'];
	}
	$return['list']=array();
	$p_group=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','parentid'=>'0'),'','paixu asc');
	//子分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['saletype likes']=array('1');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','image','price','total','active_plugin','active_id');
	if($p_group){
		$i=0;
		foreach($p_group as &$item){
			$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','isrecommand'=>'1','parentid'=>$item['id']),'','paixu asc');
			$item['goodslist']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('1'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$item['goodsnew']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('2'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$i=$i+1;
			$item['i']=$i;
			if($item['c_group']){
				foreach($item['c_group'] as &$row){
					$params['ccate']=$row['id'];
					$row['goods']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
				}
			}
		}
	}
	$return['sale_live']=$sale_live;
	//获取用户信息
	$return['extradata']=array();
	$return['user']=model('core/member')->basic($_SESSION['uid']);
	$return['login']=$_SESSION['uid']?1:2;
	$return['notice_list']=$notice_list;
	$return['p_group']=$p_group;
	$return['params']['pid']=0;
	$return['live_status']=array(0=>'未知',101=>'直播中',102=>'未开始',103=>'已结束',104=>'禁播',105=>'暂停',106=>'异常',107=>'已过期');
	$sharelink=$current_plugin.'/index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
				'title'=>$return['sale_live']['share_title']?$return['sale_live']['share_title']:'首页-小程序直播商城',
				'desc'=>$return['sale_live']['share_desc']?$return['sale_live']['share_desc']:'小程序直播商城，一个属于自己的直播商城！',
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>tomedia($return['sale_live']['share_image'])
	);
	set_html('title',$return['sale_live']['title']?:'小程序直播商城');
	exi($return);
}
if($_RQ['op']=='getlist'){
	if($_SESSION['wxapp_id']){
		$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$this->session('live_id',$live_id);
	}
	$live_id=$this->session('live_id');
	$sale_live=$this->plugin_setting('sale_live');
	if($live_id){
		if($sale_live['live_id']){
			$live_id=$sale_live['live_id'];
		}else{
			$live_id=pdo_getcolumn('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$this->session('live_id',$live_id);
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id,'ischeck'=>1);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$orderby='start_time desc, id desc';
	if($_RQ['pid']){
		if($_RQ['pid']==0){
		}elseif($_RQ['pid']==1){
			$params['sex']=2;
		}elseif($_RQ['pid']==2){
			$params['sex']=1;
		}elseif($_RQ['pid']==3){//成交额
			$orderby='totalfee desc,id desc';
		}elseif($_RQ['pid']==4){//拉新
			$orderby='fromlook desc,id desc';
		}
		$return['params']['pid']=$_RQ['pid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = intval($_RQ['num'])?:12;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live_rooms',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		$live_status=array(0=>'未知',101=>'直播中',102=>'未开始',103=>'已结束',104=>'禁播',105=>'暂停',106=>'异常',107=>'已过期');
		$goods_ids=array();
		foreach($list as &$item){
			$item['image']=$item['feeds_img']?:$item['cover_img'];
			$item['image']=tomedia($item['image']);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['start_time']=date('Y/m/d H:i:s',$item['start_time']);
			$item['grouptitle']=$fenlei[$item['group_id']]['title'];
			$item['member']=model('core/member')->detail($item['mid'],array('credit1','credit2','mobile','avatar','nickname'));
			$item['live_statustitle']=$live_status[$item['live_status']];
			if($item['live_status'] && $item['live_status']!=102 && $sale_live['istotalfee'] && ($item['totalfee']<$sale_live['mintotalfee'] || !$sale_live['mintotalfee']) && $sale_live['maxtotalfee']){
				$item['totalfee']=rand(intval($sale_live['mintotalfee']),intval($sale_live['maxtotalfee']));
				pdo_update('core_account_wxapp_live_rooms',array('totalfee'=>$item['totalfee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if($item['live_status']==0 || $item['live_status']==102){
				$item['totalfee']=0;
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='uproomids'){
	$uproomids=get_postdata();
	$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
	foreach($uproomids as $item){
		pdo_update('core_account_wxapp_live_rooms',array('live_status'=>$item['live_status']),array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id,'roomid'=>$item['roomid']));
	}
	$this->model('live')->checkendtime($live_id);
	exi('更新成功！');
}
if($_RQ['op']=='uplooktime'){
	$id=$_RQ['id'];
	$rooms=pdo_get('core_account_wxapp_live_rooms',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($rooms['id'] && $rooms['live_status']<102){
		$this->model('live')->uplooklog($rooms,$_RQ['fromuser']);
	}
	exi('更新成功！');
}
if($_RQ['op']=='startlooktime'){
	$this->model('live')->startlooktime();
	exi('更新成功！');
}
if($_RQ['op']=='endlooktime'){
	$this->model('live')->endlooktime();
	exi('更新成功！');
}
if($_RQ['op']=='like'){
	exi('点赞成功！');
}
?>